var e = require;
var t = module;
var o = exports;
var n,
    i = o,
    a =
        ((n = function (e, t) {
            return (n =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (e, t) {
                        e.__proto__ = t;
                    }) ||
                function (e, t) {
                    for (var o in t) t.hasOwnProperty(o) && (e[o] = t[o]);
                })(e, t);
        }),
        function (e, t) {
            function o() {
                this.constructor = e;
            }
            n(e, t), (e.prototype = null === t ? Object.create(t) : ((o.prototype = t.prototype), new o()));
        }),
    r = function (e, t, o, n) {
        return new (o || (o = Promise))(function (i, a) {
            function r(e) {
                try {
                    c(n.next(e));
                } catch (t) {
                    a(t);
                }
            }
            function s(e) {
                try {
                    c(n.throw(e));
                } catch (t) {
                    a(t);
                }
            }
            function c(e) {
                var t;
                e.done
                    ? i(e.value)
                    : ((t = e.value),
                      t instanceof o
                          ? t
                          : new o(function (e) {
                                e(t);
                            })).then(r, s);
            }
            c((n = n.apply(e, t || [])).next());
        });
    },
    s = function (e, t) {
        var o,
            n,
            i,
            a,
            r = {
                label: 0,
                sent: function () {
                    if (1 & i[0]) throw i[1];
                    return i[1];
                },
                trys: [],
                ops: []
            };
        return (
            (a = {next: s(0), throw: s(1), return: s(2)}),
            "function" == typeof Symbol &&
                (a[Symbol.iterator] = function () {
                    return this;
                }),
            a
        );
        function s(e) {
            return function (t) {
                return c([e, t]);
            };
        }
        function c(a) {
            if (o) throw new TypeError("Generator is already executing.");
            for (; r; )
                try {
                    if (
                        ((o = 1),
                        n &&
                            (i = 2 & a[0] ? n.return : a[0] ? n.throw || ((i = n.return) && i.call(n), 0) : n.next) &&
                            !(i = i.call(n, a[1])).done)
                    )
                        return i;
                    switch (((n = 0), i && (a = [2 & a[0], i.value]), a[0])) {
                        case 0:
                        case 1:
                            i = a;
                            break;
                        case 4:
                            return r.label++, {value: a[1], done: !1};
                        case 5:
                            r.label++, (n = a[1]), (a = [0]);
                            continue;
                        case 7:
                            (a = r.ops.pop()), r.trys.pop();
                            continue;
                        default:
                            if (!(i = (i = r.trys).length > 0 && i[i.length - 1]) && (6 === a[0] || 2 === a[0])) {
                                r = 0;
                                continue;
                            }
                            if (3 === a[0] && (!i || (a[1] > i[0] && a[1] < i[3]))) {
                                r.label = a[1];
                                break;
                            }
                            if (6 === a[0] && r.label < i[1]) {
                                (r.label = i[1]), (i = a);
                                break;
                            }
                            if (i && r.label < i[2]) {
                                (r.label = i[2]), r.ops.push(a);
                                break;
                            }
                            i[2] && r.ops.pop(), r.trys.pop();
                            continue;
                    }
                    a = t.call(e, r);
                } catch (s) {
                    (a = [6, s]), (n = 0);
                } finally {
                    o = i = 0;
                }
            if (5 & a[0]) throw a[1];
            return {value: a[0] ? a[1] : void 0, done: !0};
        }
    };
Object.defineProperty(i, "__esModule", {value: !0});
var c = e("BundleManager"),
    l = (function (e) {
        function t() {
            var t = (null !== e && e.apply(this, arguments)) || this;
            return (
                (t._bundleNames = []), (t._currLoadIndex = 0), (t._successCallBack = null), (t._failCallBack = null), t
            );
        }
        return (
            a(t, e),
            (t.prototype.execute = function (e) {
                var t = e.getBody();
                t &&
                    (t.failCallBack && (this._failCallBack = t.failCallBack),
                    t.finishCallBack && (this._successCallBack = t.finishCallBack),
                    t.bundleNames && (this._bundleNames = t.bundleNames)),
                    this.startLoadBundles();
            }),
            (t.prototype.startLoadBundles = function () {
                game.log("LoadBundlesCommand", "startLoadBundles  " + JSON.stringify(this._bundleNames)),
                    this._bundleNames && this._bundleNames.length > 0 ? this.toNextLoad() : this.onLoadBundlesSuccess();
            }),
            (t.prototype.toNextLoad = function () {
                var e = this;
                this._currLoadIndex < this._bundleNames.length
                    ? this.loadBundle(this._bundleNames[this._currLoadIndex])
                          .then(function () {
                              e._currLoadIndex++, e.toNextLoad();
                          })
                          .catch(function (t) {
                              e.onLoadBundlesFail(t);
                          })
                    : this.onLoadBundlesSuccess();
            }),
            (t.prototype.loadBundle = function (e) {
                return r(this, void 0, Promise, function () {
                    var t = this;
                    return s(this, function () {
                        return [
                            2,
                            new Promise(function (o, n) {
                                c.default.instance.loadBundle(
                                    e,
                                    function (e) {
                                        t.onLoadBundlesProgress(e);
                                    },
                                    function () {
                                        o(null);
                                    },
                                    function (e) {
                                        n(e);
                                    }
                                );
                            })
                        ];
                    });
                });
            }),
            (t.prototype.onLoadBundlesProgress = function (e) {
                this.sendNotification(t.LOAD_BUNDLES_PROGRESS, {
                    progress: e,
                    totalBundleCount: this._bundleNames.length,
                    currBundleIndex: this._currLoadIndex + 1
                });
            }),
            (t.prototype.onLoadBundlesSuccess = function () {
                this._successCallBack && this._successCallBack();
            }),
            (t.prototype.onLoadBundlesFail = function (e) {
                game.error("LoadResCommand", "loadBundes error" + e),
                    this._failCallBack &&
                        this._failCallBack({
                            error_code: game.LoadingAssetsErrorCode.LoadingResourceError,
                            error_msg: "资源加载失败"
                        });
            }),
            (t.LOAD_BUNDLES_PROGRESS = "LOAD_BUNDLES_PROGRESS"),
            t
        );
    })(puremvc.SimpleCommand);
i.default = l;
